package edu.zhku.ttms.view.dialog;

import edu.zhku.ttms.domain.Ticket;
import edu.zhku.ttms.service.CarService;
import edu.zhku.ttms.service.TicketService;
import edu.zhku.ttms.service.impl.CarServiceImpl;
import edu.zhku.ttms.service.impl.TicketServiceImpl;
import edu.zhku.ttms.utils.CheckUtil;
import edu.zhku.ttms.view.ViewController;
import edu.zhku.ttms.view.font.LeftLabelFont;
import edu.zhku.ttms.view.panel.component.DateTimePanel;
import edu.zhku.ttms.view.panel.content.TicketManagementPanel;

import javax.swing.*;
import java.awt.*;
import java.math.BigDecimal;
import java.util.List;

/**
 * @author 林志强
 * @since 2021/4/27 21:09
 */
public class TicketModDialog extends JDialog {

    static TicketService ticketService = new TicketServiceImpl();

    static CarService carService = new CarServiceImpl();

    public TicketModDialog(Ticket ticket) {
        super(ViewController.getFrame(), "罚单录入", true);
        this.setLayout(null);
        this.setBounds(530, 345, 500, 250);

        Font font = new LeftLabelFont();

        int x = 10;
        int y = 0;
        int fieldWidth = 300;
        int labelWidth = 100;
        int buttonWidth = 100;
        int height = 25;
        int margin = 5;

        int incrementY = height + margin;

        JLabel licensePlateNumberLabel = new JLabel("车牌号");
        licensePlateNumberLabel.setFont(font);
        licensePlateNumberLabel.setBounds(x, y, labelWidth, height);
        this.add(licensePlateNumberLabel);

        JComboBox<String> licensePlateNumberInput = new JComboBox<>();
        List<String> licensePlateNumberList = carService.getLicensePlateNumberList();
        for (String licensePlateNumber : licensePlateNumberList) {
            licensePlateNumberInput.addItem(licensePlateNumber);
        }
        licensePlateNumberInput.setBounds(x + labelWidth, y, fieldWidth, height);
        licensePlateNumberInput.setSelectedItem(ticket.getCar().getLicensePlateNumber());
        this.add(licensePlateNumberInput);

        JLabel driverLicenseNumberLabel = new JLabel("驾驶证号");
        driverLicenseNumberLabel.setFont(font);
        driverLicenseNumberLabel.setBounds(x, y + incrementY, labelWidth, height);
        this.add(driverLicenseNumberLabel);

        JTextField driverLicenseNumberInput = new JTextField();
        driverLicenseNumberInput.setText(ticket.getDriverLicenseNumber());
        driverLicenseNumberInput.setBounds(x + labelWidth, y + incrementY, fieldWidth, height);
        this.add(driverLicenseNumberInput);

        JLabel violationTimeLabel = new JLabel("违章时间");
        violationTimeLabel.setFont(font);
        violationTimeLabel.setBounds(x, y + 2 * incrementY, labelWidth, height);
        this.add(violationTimeLabel);

        DateTimePanel violationTimeInput = new DateTimePanel(ticket.getViolationTime());
        violationTimeInput.setBounds(x + labelWidth, y + 2 * incrementY, 375, height);
        this.add(violationTimeInput);

        JLabel violationPlaceLabel = new JLabel("违章地点");
        violationPlaceLabel.setFont(font);
        violationPlaceLabel.setBounds(x, y + 3 * incrementY, labelWidth, height);
        this.add(violationPlaceLabel);

        JTextField violationPlaceInput = new JTextField();
        violationPlaceInput.setText(ticket.getViolationPlace());
        violationPlaceInput.setBounds(x + labelWidth, y + 3 * incrementY, fieldWidth, height);
        this.add(violationPlaceInput);

        JLabel amountLabel = new JLabel("罚款金额");
        amountLabel.setFont(font);
        amountLabel.setBounds(x, y + 4 * incrementY, labelWidth, height);
        this.add(amountLabel);

        SpinnerModel amountSpinnerModel = new SpinnerNumberModel(ticket.getAmount().doubleValue(), 0.01, 999.99, 0.01);
        JSpinner amountInput = new JSpinner(amountSpinnerModel);
        amountInput.setBounds(x + labelWidth, y + 4 * incrementY, fieldWidth, height);
        this.add(amountInput);

        JLabel tip = new JLabel();
        tip.setForeground(Color.RED);
        tip.setFont(font);
        tip.setBounds(x + labelWidth, y + 5 * incrementY, fieldWidth, height);
        this.add(tip);

        JButton ticketModButton = new JButton("修改");
        ticketModButton.addActionListener(e -> {
            ticket.setCarId(carService.getByLicensePlateNumber((String)licensePlateNumberInput.getSelectedItem()).getCarId());
            ticket.setDriverLicenseNumber(driverLicenseNumberInput.getText());
            ticket.setViolationTime(violationTimeInput.getTime());
            ticket.setViolationPlace(violationPlaceInput.getText());
            ticket.setAmount(new BigDecimal(Double.toString((double)amountInput.getValue())));
            String res = ticketService.save(ticket);
            if (CheckUtil.isSuccess(res)) {
                this.setVisible(false);
                ViewController.changeView(new TicketManagementPanel());
            } else {
                tip.setText(res);
            }
        });
        ticketModButton.setBounds(x + labelWidth, y + 6 * incrementY, buttonWidth, height);
        this.add(ticketModButton);
    }

}
